{include file='public/header' /}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">

            <div class="layui-form">
                <div class="layui-input-inline">
                    <input type="text" name="title" id="title" placeholder="请输入用户动态标题" class="layui-input">
                </div>
                <button class="layui-btn" id="searchBtn"><i class="layui-icon">&#xe615;</i>搜 索</button>
            </div>
        </div>
    </div>
</div>

<!-- 表格操作列 -->

<script type="text/html" id="tableBarUser">
    <a class="layui-btn layui-btn-primary layui-btn-xs {:auth('user.topic/edit')}" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs {:auth('user.topic/delete')}" lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<div class="layui-card-body">

    <table id="dataTable" lay-filter="dataTable"></table>

    <script type="text/html" id="tableStatusUser">
        <div class="layui-btn-group">
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">驳回</a>
        </div>
    </script>

    <!-- 表格状态列 -->
    <script type="text/html" id="tableStateUser">
        <input type="checkbox" lay-filter="ckStateUser" value="{{d.id}}" lay-skin="switch"
               lay-text="通过|待审核" {{d.status==1?'checked':''}}/>
    </script>


    <script type="text/html" id="user">
        {{# if(d.user){ }}
        {{d.user.nick_name}}
        {{# }else{ }}
        用户名不存在
        {{#  } }}
    </script>
    <script type="text/html" id="topic">
        {{# if(d.topic){ }}
        {{d.topic.title}}
        {{# }else{ }}
        用户动态不存在
        {{#  } }}
    </script>
</div>
<!-- js部分 -->
{include file='public/footer' /}
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
//用户表格初始化
        var dataTable =
                table.render({
                    elem: '#dataTable'
                    , autoSort: false
                    , height: 500
                    , url: '{:__url("user.comment/getData")}' //数据接口
                    , page: true //开启分页
                    , cols: [[ //表头
                        {checkbox: true, fixed: true}
                        , {field: 'id', title: 'ID', sort: true, width: 80}
                        , {field: 'user', title: '用户', toolbar: '#user',width: 200}
                        , {field: 'title', title: '帖子标题', toolbar: '#topic'}
                        , {field: 'content', title: '评论内容'}

//                        , {
//                            title: '状态', templet: function (d){
//                                var strs = ['<span class="layui-badge layui-bg-blue">待审核</span>','<span class="layui-badge layui-bg-green">已通过</span>', '<span class="layui-badge layui-bg-red">已驳回</span>'];
//                                return strs[d.status];
//                            }, align: 'center'
//                        }

                        , {field: 'create_time', title: '评论时间', width: 160}
                        ,{field: 'status', sort: true, templet: '#tableStateUser', title: '审核操作',width: 100}

                        , {fixed: 'right', width: 140, align: 'center', toolbar: '#tableStatusUser', title: '驳回操作'}
                    ]]
                });

        //监听工具条
        table.on('tool(dataTable)', function (obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') {
                layer.confirm('确认驳回吗？', function (index) {
                    layer.close(index);
                    var load = layer.load();
                    $.post("{:url('user.comment/action')}", {
                        _method: 'delete',
                        id: data.id,
                        status:2
                    }, function (res) {
                        layer.close(load);
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1}, function () {
                                obj.del();
                            })
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                showEditModel('预览'+data.title,'/admin/user.topic/edit?id=' + data.id);

            }
        });
//        // 渲染表格
//        var insTb = table.render({
//            elem: '#tableUser',
//            url: '{:__url("user.topic/getData")}',
//            page: true,
//            toolbar: true,
//            cellMinWidth: 100,
//            cols: [[ //表头
//                {checkbox: true, fixed: true}
//                , {field: 'id', title: 'ID', sort: true, width: 80}
//                , {field: 'user', title: '用户', toolbar: '#store'}
//                , {field: 'title', title: '帖子标题'}
//                ,{
//                    title: '状态', templet: function (d){
//                        var strs = ['<span class="layui-badge layui-bg-blue">待审核</span>','<span class="layui-badge layui-bg-green">已通过</span>', '<span class="layui-badge layui-bg-red">已驳回</span>'];
//                        return strs[d.status];
//                    }, align: 'center'
//                }
//                , {field: 'views', title: '点击量'}
//                , {field: 'sort',sort: true, title: '排序'}
//                , {field: 'create_time', title: '创建时间', width: 160}
//                /*, {field: 'updated_at', title: '更新时间'}*/
//
//                ,{field: 'status', sort: true, templet: '#tableStateUser', title: '审核操作'}
//                ,{field: 'is_top', sort: true,width:100, templet: '#tableStateUser1', title: '置顶操作'}
//
//                , {fixed: 'right', width: 140, align: 'center', toolbar: '#tableStatusUser', title: '预览详情'}
//
//            ]]
//        });
//
//        //监听工具条
//        table.on('tool(tableUser)', function (obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
//         console.log(123);
//            var data = obj.data //获得当前行数据
//                    , layEvent = obj.event; //获得 lay-event 对应的值
//            if (layEvent === 'del') {
//                layer.confirm('确认删除吗？', function (index) {
//                    layer.close(index);
//                    var load = layer.load();
//                    $.post("{:__url('user.topic/delete')}", {
//                        _method: 'delete',
//                        ids: [data.id]
//                    }, function (res) {
//                        layer.close(load);
//                        if (res.code == 0) {
//                            layer.msg(res.msg, {icon: 1}, function () {
//                                obj.del();
//                            })
//                        } else {
//                            layer.msg(res.msg, {icon: 2})
//                        }
//                    });
//                });
//            } else if (layEvent === 'edit') {
//
//                showEditModel('预览'+data.title,'/admin/user.topic/edit?id=' + data.id);
//
//            }
//        });



        // 显示表单弹窗
        function showEditModel(title,url) {
            admin.open({
                type: 2,
                offset:'r',
                title: title,
                content: url,
                area: ['600px', '100%'],
                btn:['通过','驳回'],
                btnAlign: 'l',   // 按钮居中
                success: function (layero, dIndex) {
                    form.render();    // 表单渲染
                },

                yes: function (index, layero) {
// 获取弹出层中的form表单元素
                    var formSubmit=layer.getChildFrame('form', index);
                    //获取表单数据
                    var data = {status:1};
                    var action = "{:__url('user.comment/action')}";
                    var a = formSubmit.serializeArray();
                    $.each(a, function () {
                        if (data[this.name] !== undefined) {
                            if (!data[this.name].push) {
                                data[this.name] = [data[this.name]];
                            }
                            data[this.name].push(this.value || '');
                        } else {
                            data[this.name] = this.value || '';
                        }
                    });

                    layer.load(2);
                    $.post(action, data, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(index);

                            dataTable.reload({
                                page: {curr: 1}
                            })

                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'json');
                    return false;

                },
                btn2: function (index, layero) {
                    // 获取弹出层中的form表单元素
                    var formSubmit=layer.getChildFrame('form', index);
                    //获取表单数据
                    var data = {status:2};
                    var action = "{:__url('user.comment/action')}";
                    var a = formSubmit.serializeArray();
                    $.each(a, function () {
                        if (data[this.name] !== undefined) {
                            if (!data[this.name].push) {
                                data[this.name] = [data[this.name]];
                            }
                            data[this.name].push(this.value || '');
                        } else {
                            data[this.name] = this.value || '';
                        }
                    });

                    layer.load(2);
                    $.post(action, data, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(index);

                            dataTable.reload({
                                page: {curr: 1}
                            })

                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'json');
                    return false;
                }


            });
        }

        form.on('switch(ckStateUser1)', function (obj) {
            layer.load(2);
            $.post("{:__url('user.comment/action')}", {
                id: obj.elem.value,
                is_top: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 0) {

                    layer.msg(res.msg, {icon: 1});

                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });
        //审核
        //
        form.on('switch(ckStateUser)', function (obj) {
            layer.load(2);
            $.post("{:__url('user.comment/action')}", {
                id: obj.elem.value,
                status: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                    dataTable.reload({page: {curr: 1}});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

        //搜索
        $("#searchBtn").click(function () {
            var catId = $("#category_id").val();
            var title = $("#title").val();
            dataTable.reload({
                where: {category_id: catId, title: title},
                page: {curr: 1}
            })
        })

    });
</script>

</body>
</html>
